<?php
namespace plugins\store\interfaceLib;
use cmf\controller\PluginAdminBaseController;
use think\Db;
use plugins\store\model\StoreModel;
use plugins\member\model\MemberModel;
use plugins\finance\model\FinanceModel;

class StoreClassify extends PluginAdminBaseController
{
	public function createStoreAdmin($store_id)
	{
		$store = StoreModel::get(['store_id'=>$store_id]);
		// if($store->store_status == 1){
			Db::startTrans();
			try{
				//修改店铺状态
				$store->store_status = 2;
				$store->save();
				//取得member用户信息
				$uid = $store->uid;
				$member = MemberModel::get(['uid'=>$uid]);
				//后台user中添加账号
				//user表添加
				$userid = db('user')->insertGetId(['user_type'=>3,'user_login'=>$member->phone,'user_pass'=>$member->password]);

				//user_attach表添加

				db('user_attach')->insert(['user_id'=>$userid,'company_id'=>0,'department_id'=>0,'role_id'=>3]);

				//store_member_admin表添加

				db('store_member_admin')->insert(['user_id'=>$userid,'member_id'=>$member->uid,'store_id'=>$store_id,'role_id'=>3]);

				//添加财务账户

				( new FinanceModel)->createFinanceAccount('store',$store_id);



				Db::commit();

				return true;

			}catch(\Exception $e){

				Db::rollback();

			}

		// }



		return false;



	}



}